<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      canvas {
        border: 1px solid #ccc;
        margin-right: 5px;
      }
    </style>
  </head>
  <body>
    <script>
       (()=>{
        const canvas = document.createElement("canvas");
        canvas.width = 400;
        canvas.height = 400;
        document.body.append(canvas);

        const ctx = canvas.getContext("2d");

        //填充一个红色的矩形
        ctx.save()
        ctx.fillStyle = '#0f0';
        ctx.beginPath();
        ctx.fillRect(0,0,100,100);

        //填充一个蓝色的圆
        ctx.save();
        ctx.fillStyle = '#00f';
        ctx.beginPath();
        ctx.arc(200,200,100,0,Math.PI*2);
        ctx.fill();

        
        //填充一个红色的矩形
        //ctx.fillStyle = '#f00';
        ctx.restore();
        ctx.fillRect(300,300,100,100);
       })();


       (()=>{
        const canvas = document.createElement("canvas");
        canvas.width = 400;
        canvas.height = 400;
        document.body.append(canvas);

        const ctx = canvas.getContext("2d");

        //绘制一个矩形，移动至(100,100)
        ctx.save();
        ctx.translate(100,100);
        ctx.fillStyle = '#f00';
        ctx.fillRect(0,0,100,100);
        ctx.restore();

        //绘制一个矩形，旋转45°
        //ctx.translate(-100,-100);
        ctx.restore();
        ctx.fillStyle = '#0f0';
        ctx.beginPath();
        ctx.fillRect(0,0,100,100);

       })();


    </script>

  </body>
</html>
